package com.tom.demo.array;

import java.util.Arrays;

public class Test283_MoveZeroes {
    public static void main(String[] args) {
        int[] arrs = {0, 1, 0, 3, 12};
        moveZeroes2(arrs);
        System.out.println(Arrays.toString(arrs));
    }

//    示例 1:
//    输入: nums = [0,1,0,3,12]
//    输出: [1,3,12,0,0]

    //    示例 2:
//    输入: nums = [0]
//    输出: [0]
    public static void moveZeroes(int[] nums) {
        int j = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                nums[j] = nums[i];
                if (i != j) {
                    nums[i] = 0;
                }
                j++;
            }
        }
    }

    public static void moveZeroes2(int[] nums) {
        //    示例 1:
        //    输入: nums = [0,1,0,3,12]
        //    输出: [1,3,12,0,0]

        int j = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                nums[j] = nums[i];
                if (j != i) {
                    nums[i] = 0;
                }
                j++;
            }
        }
    }
}
